package hairmember

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/hairmember"
	hairmemberReq "github.com/flipped-aurora/gin-vue-admin/server/model/hairmember/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type MemberLevelsApi struct{}

// CreateMemberLevels 创建memberLevels表
// @Tags MemberLevels
// @Summary 创建memberLevels表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body hairmember.MemberLevels true "创建memberLevels表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /memberLevels/createMemberLevels [post]
func (memberLevelsApi *MemberLevelsApi) CreateMemberLevels(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var memberLevels hairmember.MemberLevels
	err := c.ShouldBindJSON(&memberLevels)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = memberLevelsService.CreateMemberLevels(ctx, &memberLevels)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteMemberLevels 删除memberLevels表
// @Tags MemberLevels
// @Summary 删除memberLevels表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body hairmember.MemberLevels true "删除memberLevels表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /memberLevels/deleteMemberLevels [delete]
func (memberLevelsApi *MemberLevelsApi) DeleteMemberLevels(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	id := c.Query("id")
	err := memberLevelsService.DeleteMemberLevels(ctx, id)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteMemberLevelsByIds 批量删除memberLevels表
// @Tags MemberLevels
// @Summary 批量删除memberLevels表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /memberLevels/deleteMemberLevelsByIds [delete]
func (memberLevelsApi *MemberLevelsApi) DeleteMemberLevelsByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ids := c.QueryArray("ids[]")
	err := memberLevelsService.DeleteMemberLevelsByIds(ctx, ids)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateMemberLevels 更新memberLevels表
// @Tags MemberLevels
// @Summary 更新memberLevels表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body hairmember.MemberLevels true "更新memberLevels表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /memberLevels/updateMemberLevels [put]
func (memberLevelsApi *MemberLevelsApi) UpdateMemberLevels(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var memberLevels hairmember.MemberLevels
	err := c.ShouldBindJSON(&memberLevels)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = memberLevelsService.UpdateMemberLevels(ctx, memberLevels)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindMemberLevels 用id查询memberLevels表
// @Tags MemberLevels
// @Summary 用id查询memberLevels表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param id query int true "用id查询memberLevels表"
// @Success 200 {object} response.Response{data=hairmember.MemberLevels,msg=string} "查询成功"
// @Router /memberLevels/findMemberLevels [get]
func (memberLevelsApi *MemberLevelsApi) FindMemberLevels(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	id := c.Query("id")
	memberLevels, err := memberLevelsService.GetMemberLevels(ctx, id)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(memberLevels, c)
}

// GetMemberLevelsList 分页获取memberLevels表列表
// @Tags MemberLevels
// @Summary 分页获取memberLevels表列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query hairmemberReq.MemberLevelsSearch true "分页获取memberLevels表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /memberLevels/getMemberLevelsList [get]
func (memberLevelsApi *MemberLevelsApi) GetMemberLevelsList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo hairmemberReq.MemberLevelsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := memberLevelsService.GetMemberLevelsInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetMemberLevelsPublic 不需要鉴权的memberLevels表接口
// @Tags MemberLevels
// @Summary 获取所有启用的会员等级列表（不需要鉴权）
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=[]hairmember.MemberLevels,msg=string} "获取成功"
// @Router /memberLevels/getMemberLevelsPublic [get]
func (memberLevelsApi *MemberLevelsApi) GetMemberLevelsPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	list, err := memberLevelsService.GetMemberLevelsPublic(ctx)
	if err != nil {
		response.FailWithMessage("获取会员等级列表失败", c)
		return
	}
	response.OkWithDetailed(list, "获取成功", c)
}
